package cn.justforchris.blog.controller;


import cn.justforchris.blog.domain.Param;
import cn.justforchris.blog.query.QueryPage;
import cn.justforchris.blog.service.IParamService;
import cn.justforchris.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/param")
@Api(tags = "文章类型接口")
public class ParamController {

    @Autowired
    private IParamService paramService;

    /**
     * 新增
     * @param
     * @return
     */
    @PutMapping
    public JsonResult insertOrUpdate(@RequestBody Param param) {
       try {
           Date date = new Date();
           param.setUpdateTime(date);
           if (param.getId() == null) {
            paramService.insertParam(param);
        } else {
            paramService.updateParam(param);
        }
        return JsonResult.success();
       }catch (Exception e) {
           e.printStackTrace();
           return JsonResult.error();
       }
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deletParam(@PathVariable Long id){
        try{
            paramService.deleteParam(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDelet(@RequestBody Long[] ids){
        try{
            paramService.patchDelete(ids);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Param getParam(@PathVariable Long id){

        return paramService.getParamById(id);

    }

    /**
     * 获取所有
     * @return
     */
    @GetMapping("/getParams")
    public Map<String,Object> getAll(){
        return paramService.getAll();
    }

    /**
     * 分页查询
     * @param queryPage
     * @return
     */
    @PostMapping
    public PageInfo<Param> queryPage(@RequestBody QueryPage queryPage){
        /*返回的数据是pagehelper的一个类,这个类创建的对象中储存的是查询出来的数据以及总条数(total)*/
        return paramService.queryPage(queryPage);

    }









}
